package org.example.innovate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.example.innovate.common.CommonResponse;
import org.example.innovate.domain.IngredientsInventory;
import org.example.innovate.domain.User;
import org.example.innovate.service.IngredientsInventoryService;
import org.example.innovate.mapper.IngredientsInventoryMapper;
import org.example.innovate.util.JwtUtil;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
* @author 马鑫
* @description 针对表【ingredients_inventory】的数据库操作Service实现
* @createDate 2024-12-31 17:25:03
*/
@Service
public class IngredientsInventoryServiceImpl extends ServiceImpl<IngredientsInventoryMapper, IngredientsInventory>
    implements IngredientsInventoryService{
    @Resource
    private IngredientsInventoryMapper ingredientsInventoryMapper;

    @Override
    public CommonResponse<List<IngredientsInventory>> get(HttpServletRequest req) {
        String token=req.getHeader("token");
        JwtUtil.verifyToken(token);
        User user= JwtUtil.getCurrentUser(token);
        if(user==null)
        {
            return CommonResponse.createForError("用户不存在");
        }
        List<IngredientsInventory> ingredientsInventories=new ArrayList<>();
        QueryWrapper<IngredientsInventory> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",user.getUserId());
        queryWrapper.orderByDesc("created_at");
        ingredientsInventories=ingredientsInventoryMapper.selectList(queryWrapper);
        for(IngredientsInventory ingredientsInventory:ingredientsInventories)
        {
            Calendar cal = Calendar.getInstance();
            cal.setTime(ingredientsInventory.getCreatedAt());
            cal.add(Calendar.HOUR_OF_DAY, 8);
            Date eightHoursAgo = cal.getTime();
            ingredientsInventory.setCreatedAt(eightHoursAgo);
            cal.setTime(ingredientsInventory.getUpdatedAt());
            cal.add(Calendar.HOUR_OF_DAY, 8);
            eightHoursAgo = cal.getTime();
            ingredientsInventory.setUpdatedAt(eightHoursAgo);
        }
        return CommonResponse.createForSuccess("查询成功",ingredientsInventories);
    }

    @Override
    public CommonResponse<IngredientsInventory> add(HttpServletRequest req, IngredientsInventory ingredientsInventory) {
        String token=req.getHeader("token");
        JwtUtil.verifyToken(token);
        User user= JwtUtil.getCurrentUser(token);
        if(user==null)
        {
            return CommonResponse.createForError("用户不存在");
        }
        ingredientsInventory.setUserId(user.getUserId());
        Calendar cal = Calendar.getInstance();
        cal.setTime(ingredientsInventory.getCreatedAt());
        cal.add(Calendar.HOUR_OF_DAY, -8);
        Date eightHoursAgo = cal.getTime();
        ingredientsInventory.setCreatedAt(eightHoursAgo);
        ingredientsInventoryMapper.insert(ingredientsInventory);
        cal = Calendar.getInstance();
        cal.setTime(ingredientsInventory.getCreatedAt());
        cal.add(Calendar.HOUR_OF_DAY, 8);
        eightHoursAgo = cal.getTime();
        ingredientsInventory.setCreatedAt(eightHoursAgo);
        Date now = new Date();
        ingredientsInventory.setUpdatedAt(now);
        return CommonResponse.createForSuccess("添加成功",ingredientsInventory);
    }

    @Override
    public CommonResponse<List<IngredientsInventory>> update(List<IngredientsInventory> ingredientsInventorys,HttpServletRequest req) {
        String token=req.getHeader("token");
        JwtUtil.verifyToken(token);
        User user= JwtUtil.getCurrentUser(token);
        if(user==null)
        {
            return CommonResponse.createForError("用户不存在");
        }
        for(IngredientsInventory ingredientsInventory:ingredientsInventorys)
        {
            ingredientsInventory.setUserId(user.getUserId());
            ingredientsInventoryMapper.updateById(ingredientsInventory);
            QueryWrapper<IngredientsInventory> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("inventory_id",ingredientsInventory.getInventoryId());
            IngredientsInventory ingredientsInventory1=ingredientsInventoryMapper.selectOne(queryWrapper);
            Calendar cal = Calendar.getInstance();
            cal.setTime(ingredientsInventory1.getCreatedAt());
            cal.add(Calendar.HOUR_OF_DAY, 8);
            Date eightHoursAgo = cal.getTime();
            ingredientsInventory.setCreatedAt(eightHoursAgo);
            cal.setTime(ingredientsInventory1.getUpdatedAt());
            cal.add(Calendar.HOUR_OF_DAY, 8);
            eightHoursAgo = cal.getTime();
            ingredientsInventory.setUpdatedAt(eightHoursAgo);
        }
        return CommonResponse.createForSuccess("更新成功",ingredientsInventorys);
    }

    @Override
    public CommonResponse<IngredientsInventory> delete(IngredientsInventory ingredientsInventory) {
        ingredientsInventoryMapper.deleteById(ingredientsInventory.getInventoryId());
        return CommonResponse.createForSuccess("删除成功",ingredientsInventory);
    }
}




